package com.phenix.mvp.aop.aspect;

import android.view.View;

import com.phenix.mvp.R;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * Created by zhouphenix on 2017-1-5.
 */
@Aspect
public class SingleClickAspect {

    public static final int TAG_TIME_DIFFER = R.string.tag_time_differ;

    public static final long TIME_CLICK_DIFFER = 500;

    @Pointcut("execution(@com.phenix.annotation.aspect.SingleClick * *(..))")
    public void methodAnnotated(){}


    @Around("methodAnnotated()")
    public void aroundJoinPoint(ProceedingJoinPoint joinPoint)throws Throwable {
        Object arg0 = joinPoint.getArgs()[0];
        if (null != arg0 && arg0 instanceof View){
            View view = (View) arg0;
            Object tag = view.getTag(TAG_TIME_DIFFER);
            long start = tag != null?(long) tag :0;
            long end = System.currentTimeMillis();
            if (end - start > TIME_CLICK_DIFFER){
                view.setTag(TAG_TIME_DIFFER,end);
                joinPoint.proceed();
            }
        }

    }
}
